package co.edu.usbcali.pqr.dataaccess.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import co.edu.usbcali.pqr.dataaccess.api.HibernateDaoImpl;
import co.edu.usbcali.pqr.modelo.AnexosPqr;


@Scope("singleton")
@Repository("AnexosPqrDAO")
public class AnexosPqrDAO extends HibernateDaoImpl<AnexosPqr, Long>
    implements IAnexosPqrDAO {

	private static final Logger log = LoggerFactory.getLogger(AnexosPqrDAO.class);
    @Resource
    private SessionFactory sessionFactory;

    public static IAnexosPqrDAO getFromApplicationContext(
        ApplicationContext ctx) {
        return (IAnexosPqrDAO) ctx.getBean("AnexosPqrDAO");
    }
    
    @SuppressWarnings("unchecked")
   	@Override
   	public Long existeDescripcionAnexo(String pDescripcion) throws Exception {
   		Long cant = 0L;
   			try {
   			Query query = getSession().getNamedQuery("existe_descripcion_anexo");
   			query.setParameter("pDescripcion", pDescripcion);
   			List<Long> lista = query.list();

   			if (lista!=null) {
   				cant =lista.get(0);
   			}
   			
   		} catch (Exception e) {
   			log.error(e.getMessage());
   			throw e;
   		}
   		return cant;

   	}
}
